package com.mingyizhudao.app.view.mine;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.dixiang.framework.utils.AlertDialogs;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.utils.IntentHelper;
import com.mingyizhudao.app.view.MYBaseFragment;
import com.mingyizhudao.app.view.login.LoginActivity;

public class MineFragment extends MYBaseFragment{

	private TextView user_code;
	private ImageView avtar;
	private boolean  isLogin = false;
	private RelativeLayout view_loginout;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
					Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_mine, container, false); 
		initView(view);
		return view;
	}

	private void initView(View view){
		view.findViewById(R.id.view_order).setOnClickListener(onClickListener);
		view.findViewById(R.id.view_report).setOnClickListener(onClickListener);
		view.findViewById(R.id.view_problem).setOnClickListener(onClickListener);
		view.findViewById(R.id.view_about).setOnClickListener(onClickListener);
		view.findViewById(R.id.view_call).setOnClickListener(onClickListener);
		avtar = ( ImageView )view.findViewById(R.id.avtar);
		avtar.setOnClickListener(onClickListener);
		view_loginout = ( RelativeLayout )view.findViewById(R.id.view_loginout);
		view_loginout.setOnClickListener(onClickListener);
		
		user_code = ( TextView )view.findViewById(R.id.user_code);
		
	}


	@Override
	public void onResume() {
		
		String mobile =CommonUtils.getMobile( activity );
		String token =CommonUtils.getToken( activity );
		if( token!=null&&token.length()>0&&mobile!=null&&mobile.length()>0 )
		{
			isLogin = true;
			user_code.setText( mobile );
			view_loginout.setVisibility( View.VISIBLE );
			
		}else {
			user_code.setText( R.string.fm_personal_user_default_code );
			view_loginout.setVisibility( View.GONE );
		}
		super.onResume();
	}

	@Override
	public void initHeaderView() {
		clearTitle();
		setTopViewBg(R.color.white);
		setTitleViewValue( R.string.fragment_person_title, 0, R.color.actionbar_bg_color );
	}


	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
				case R.id.avtar:
					if( isLogin )
					{
						Toast.makeText( activity, "您已登录！", 0 ).show();
					}else {
						IntentHelper.getInstance(activity).gotoActivity(LoginActivity.class);
					}
					
					break;
				case R.id.view_order:
					if( isLogin )
					{
						IntentHelper.getInstance(activity).gotoActivity(OrderListActivity.class);
					}else {
						IntentHelper.getInstance(activity).gotoActivity(LoginActivity.class);
					}
					
					//IntentHelper.getInstance(activity).gotoActivity(OrderDetailActivity.class);

					break;
				case R.id.view_report:
					//IntentHelper.getInstance(activity).gotoActivity(ReportActivity.class);
					break;
					
				case R.id.view_problem:
					IntentHelper.getInstance(activity).gotoActivity(CommonProblemActivity.class);
					break;
				case R.id.view_about:
					IntentHelper.getInstance(activity).gotoActivity(AboutActivity.class);
					break;
				case R.id.view_call:
					IntentHelper.getInstance(activity).gotoActivity(CallActivity.class);
					break;
				case R.id.view_loginout:
					if(!isLogin){
						Toast.makeText( activity, "您还没登录！", 0 ).show();
					}else{
						AlertDialogs.getAlertDialog(activity).showAlertDialog("您确定要退出登录?", new OnClickListener() {
							@Override
							public void onClick(View arg0) {
								isLogin = false;
								CommonUtils.setToken( activity, null );
								user_code.setText( R.string.fm_personal_user_default_code );
								view_loginout.setVisibility( View.GONE );
							}
						});
					}
					
					break;
				default:
					break;
			}
		}
	};
	

}
